/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.recordScore.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.life.entity.OrganizeLife;
import com.fosung.frame.modules.life.entity.OrganizeLifeType;
import com.fosung.frame.modules.life.service.OrganizeLifeService;
import com.fosung.frame.modules.life.service.OrganizeLifeTypeService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 组织生活管理Controller
 * 
 * @author zhangfn
 * @version 2017-08-29
 */
@Controller
@RequestMapping(value = "${adminPath}/life/organizeLifeCx")
public class OrganizeLifeCxController extends BaseController {

	@Autowired
	private OrganizeLifeService organizeLifeService;
	@Autowired
	private OrganizeLifeTypeService organizeLifeTypeService;

	@ModelAttribute
	public OrganizeLife get(@RequestParam(required = false) String id) {
		OrganizeLife entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = organizeLifeService.get(id);
		}
		if (entity == null) {
			entity = new OrganizeLife();
		}
		return entity;
	}

	@RequiresPermissions("life:organizeLifeCx:view")
	@RequestMapping(value = { "list", "" })
	public String list(OrganizeLife organizeLife, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		organizeLife.setShstate("2");
		organizeLife.setColumn25("0");
		organizeLife.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<OrganizeLife> page = organizeLifeService.findPage(new Page<OrganizeLife>(request, response), organizeLife);
		// 给首页组织生活分类下拉框赋值
		OrganizeLifeType type = new OrganizeLifeType();
		type.setDelFlag("0");
		List<OrganizeLifeType> organizeLifeTypes = organizeLifeTypeService.findList(type);
		model.addAttribute("organizeLifeTypes", organizeLifeTypes);
		// 因为组织生活分类可能包括多个，这里先spilt再单独查询拼成字符串显示到前台list

		List<OrganizeLife> olList = page.getList();

		if (olList.size() > 0) {
			for (int i = 0; i < olList.size(); i++) {
				StringBuffer typeNames = new StringBuffer();
				OrganizeLife o = olList.get(i);
				if (o.getTypeids() != null && !o.getTypeids().equals("")) {
					String[] types = o.getTypeids().split(",");

					if (types != null && types.length > 0) {
						for (String s : types) {
							// 循环遍历数组中元素，添加到StringBuilder 对象中
							String t = organizeLifeTypeService.get(s).getTypename();
							typeNames.append(t + ",");
						}
					}
					if (typeNames.length() > 0) {
						// 调用字符串的deleteCharAt()方法,删除最后一个多余的逗号
						typeNames.deleteCharAt(typeNames.length() - 1);
					}

				}
				o.setTypeids(typeNames.toString());
			}
		}
		model.addAttribute("page", page);
		return "modules/recordScore/organizeLifeCxList";
	}

	@RequiresPermissions("life:organizeLifeJh:view")
	@RequestMapping(value = "form")
	public String form(OrganizeLife organizeLife, Model model) {
		OrganizeLifeType type = new OrganizeLifeType();
		type.setDelFlag("0");
		List<OrganizeLifeType> listStr = organizeLifeTypeService.findList(type);
		model.addAttribute("listStr", listStr);

		model.addAttribute("organizeLife", organizeLife);
		return "modules/life/organizeLifeJhForm";
	}

	

	// 组织生活提交审核
	@ResponseBody
	@RequestMapping(value = "/chehui/{ids}", method = RequestMethod.GET)
	public Map<String, String> organizeLifeTj(@PathVariable("ids") String ids, RedirectAttributes redirectAttributes) {
		Map<String, String> map = new HashMap<String, String>();

		if (ids != null && !ids.equals("")) {
			String[] organizeLifeIds = ids.split(",");

			if (organizeLifeIds != null && organizeLifeIds.length > 0) {
				OrganizeLife organizeLife = new OrganizeLife();
				for (String s : organizeLifeIds) {
					organizeLife.setId(s);
					organizeLife.setShstate("1");
					organizeLifeService.upState(organizeLife);
					map.put("type", "success");
					map.put("msg", "组织生活积分申请已撤回！");
				}
			} else {
				map.put("type", "error");
				map.put("msg", "组织生活积分申请撤回失败！");
			}
		} else {
			map.put("type", "error");
			map.put("msg", "组织生活积分申请撤回失败！");
		}

		return map;
	}

}